/*data为数据,location为位置,pagenum为显示多少页,content内容"`"+"内容"+"`"格式,nowpage现在的页码*/
var backpage=1;
var bangdingback=0;
var bangdingnext=0;
function page(data,location,pagenum,content,nowpage){
    backpage=1;
    bangdingback=0;
    bangdingnext=0;
    let num=nowpage-1
    let max=pagenum;
    if(data.length<num*max+max){
        max=data.length%pagenum
    }
    $(location).find('tbody').text('')
    for(let i=num*max;i<num*max+max;i++){
        $(location).find('tbody').append(eval(content));
    }
}
function pagenum(data,location,pagenum,content){
    let num=data.length/pagenum
    if(data.length/pagenum>3){
        num=4
    }
    $(location).text('')
    for(let i=0;i<num;i++){
        if(num>3&&i==3){
            $(location).append(`
                <li>...</li>
            `)
        }else{
            $(location).append(eval(content))
        }
    }
    if(Math.ceil(data.length/pagenum)!=1&&Math.ceil(data.length/pagenum)!=3&&Math.ceil(data.length/pagenum)!=2){
        $(location).append(`
        <li>
            ${Math.ceil(data.length/pagenum)}
        </li>
    `)
    }
    pagechecked(location)
}

function outpagenum(data,location,pagenum,content,nowpage){
    let num=parseInt(nowpage)+2
    if(num-1>=Math.ceil(data.length/pagenum)){
        num=Math.ceil(data.length/pagenum)
        if(num!=3){
            $(location).append(`
                <li>1</li>
                <li>...</li>
            `)  
        }
        for(let i=num-3;i<num;i++){
            $(location).append(eval(content))
        }
    }else{
        $(location).append(`
                <li>1</li>
                <li>...</li>
        `)
        for(let i=num-4;i<num-1;i++){
            $(location).append(eval(content))
        }
        $(location).append(`
                <li>...</li>
                <li>${Math.ceil(data.length/pagenum)}</li>
        `)
    }
}

/*data为数据,pagelocation为页码位置,contentlocation为表内容位置,pagenum为显示多少页,pagecontent页码格式,content内容"`"+"内容"+"`"格式*/
function ingo(data,pagelocation,contentloaction,pagesnum,pagecontent,content){
    $(pagelocation).off('click','li')
    $(pagelocation).on('click','li',function(){
        let gopage;
        gopage = $(this).text()
        backpage=parseInt(gopage)
        $(pagelocation).text('')
        let max=(gopage-1)*pagesnum+pagesnum;
        if(data.length<(gopage-1)*pagesnum+pagesnum){
            max=data.length
        }
        if(gopage>2){
            outpagenum(data,pagelocation,pagesnum,pagecontent,gopage)
        }else{
            pagenum(data,pagelocation,pagesnum,pagecontent)
        }
        $(contentloaction).find('tbody').text('')
        for(let i=pagesnum*(gopage-1);i<max;i++){
            $(contentloaction).find('tbody').append(eval(content));
        }
        pagechecked(pagelocation)
        back(data,pagelocation,contentloaction,pagesnum,pagecontent,content)
        next(data,pagelocation,contentloaction,pagesnum,pagecontent,content)
    })
}

//选中样式
function pagechecked(pagelocation){
    for(let i=0;i<$(pagelocation).find('li').length;i++){
        if($($(pagelocation).find('li')[i]).text()==backpage){
            $($(pagelocation).find('li')[i]).addClass('pagenumChecked')
        }
    }
}

//全选
function allcheck(){
    $('.allcheck').on('click',function(){
        if($(this).prop('checked')){
            for(let i=0;i<$('.box-checkbox').length;i++){
                $($('.box-checkbox')[i]).prop('checked', true)
            }
            for(let i=0;i<$('.allcheck').length;i++){
                $($('.allcheck')[i]).prop('checked', true)
            }
        }else{
            for(let i=0;i<$('.box-checkbox').length;i++){
                $($('.box-checkbox')[i]).prop('checked', false)
            }
            for(let i=0;i<$('.allcheck').length;i++){
                $($('.allcheck')[i]).prop('checked', false)
            }
        }
    })
}
//上一页
function back(data,pagelocation,contentloaction,pagesnum,pagecontent,content){
    if(backpage!=1){
        $('.back').removeClass('stop');
        if(bangdingback==0){
            $('.back').on('click',function(){
                let gopage=backpage
                backpage=gopage-1
                $(pagelocation).text('')
                let max=(gopage-1)*pagesnum+pagesnum;
                if(data.length<(gopage-1)*pagesnum+pagesnum){
                    max=data.length
                }
                if(gopage>2){
                    outpagenum(data,pagelocation,pagesnum,pagecontent,gopage)
                }else if(gopage==Math.ceil(data.length/pagesnum)-2){
                    backpagenum(data,pagelocation,pagesnum,pagecontent,gopage)
                }else{
                    pagenum(data,pagelocation,pagesnum,pagecontent)
                }
                $(contentloaction).find('tbody').text('')
                for(let i=pagesnum*(gopage-1);i<max;i++){
                    $(contentloaction).find('tbody').append(eval(content));
                }
                pagechecked(pagelocation)
                next(data,pagelocation,contentloaction,pagesnum,pagecontent,content)
                if(backpage==1){
                    $('.back').addClass('stop')
                    $(".back").off("click");
                    bangdingback=0
                }
            })
            bangdingback=1
        }
    }else{
        $('.back').addClass('stop')
        $(".back").off("click");
        bangdingback=0
    }
}
//下一页
function next(data,pagelocation,contentloaction,pagesnum,pagecontent,content){
    if(backpage!=Math.ceil(data.length/pagesnum)){
        $('.next').removeClass('stop');
        if(bangdingnext==0){
            $('.next').on('click',function(){
                let gopage=backpage+1
                backpage=gopage
                $(pagelocation).text('')
                let max=(gopage-1)*pagesnum+pagesnum;
                if(data.length<(gopage-1)*pagesnum+pagesnum){
                    max=data.length
                }
                if(gopage>2){
                    outpagenum(data,pagelocation,pagesnum,pagecontent,gopage)
                }else if(gopage==Math.ceil(data.length/pagesnum)-2){
                    backpagenum(data,pagelocation,pagesnum,pagecontent,gopage)
                }else{
                    pagenum(data,pagelocation,pagesnum,pagecontent)
                }
                $(contentloaction).find('tbody').text('')
                for(let i=pagesnum*(gopage-1);i<max;i++){
                    $(contentloaction).find('tbody').append(eval(content));
                }
                pagechecked(pagelocation)
                back(data,pagelocation,contentloaction,pagesnum,pagecontent,content)
                if(backpage==9){
                    $('.next').addClass('stop')
                    $(".next").off("click");
                    bangdingnext=0
                }
            })
            bangdingnext=1
        }
    }else{
        $('.next').addClass('stop')
        $(".next").off("click");
        bangdingnext=0
    }
}
//标签页
function checkshow(datas,name,condition){
    let x=0;
    if(condition!='all'){
        x=datas.filter(item => item.订单状态==condition).length
        $('.label-page').append(`
            <div>
                <span>${name}</span>
                <span>(${x})</span>
            </div>  
        `)
    }else{
        x=data.length
        $('.label-page').append(`
        <div class="hshow">
            <span>${name}</span>
            <span>(${x})</span>
        </div>  
    `)
    }
}
//初始化
function init(data,pagelocation,contentloaction,pagesnum,pagecontent,content,nowpage){
    page(data,contentloaction,pagesnum,content,nowpage)
    pagenum(data,pagelocation,pagesnum,pagecontent)
    ingo(data,pagelocation,contentloaction,pagesnum,pagecontent,content)
    back(data,pagelocation,contentloaction,pagesnum,pagecontent,content)
    next(data,pagelocation,contentloaction,pagesnum,pagecontent,content)
}